package com.pbc.gmp.action;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.pbc.gmp.commons.base.BaseController;
import com.pbc.gmp.commons.utils.StringUtils;
import com.pbc.gmp.model.RoleC;
import com.pbc.gmp.model.UserDetialInfo;
import com.pbc.gmp.model.basemodel.Role;
import com.pbc.gmp.model.basemodel.User;
import com.pbc.gmp.service.IAclService;
import com.pbc.gmp.service.IRoleService;
import com.xiaoleilu.hutool.convert.Convert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * <p>
 *   前端控制器
 * </p>
 *
 * @author kjk
 * @since 2017-04-17
 */
@Controller
@RequestMapping("/role")
public class RoleController extends BaseController {
    @Autowired private IRoleService roleService;

    @RequestMapping(value="/getrolelist")
    @ResponseBody
    public List<RoleC> getRoleList() {
        return roleService.selectRoleClist();
    }
    @RequestMapping(value="/manager")

    public String rolemanager() {
        return "admin/role/role";
    }
    @RequestMapping(value="/rolesList")
    @ResponseBody
    public List<Role> getRolesList() {
        return roleService.selectList(null);
    }
    @RequestMapping(value="/addPage")
    public String addPage() {
        return "admin/role/roleAdd";
    }
    @RequestMapping(value = "/add", method = {RequestMethod.POST })
    @ResponseBody
    public Object add(Role role) {
        Wrapper whr=new EntityWrapper();
        whr.eq("name",role.getName());
        List<Role> list = roleService.selectList(whr);
        if (list != null && !list.isEmpty()) {
            return renderError("登录名已存在!");
        }

        role.setId(0L);
        roleService.insert(role);
        return renderSuccess("添加成功");
    }
    @RequestMapping(value="/editPage")
    public String addPage(Model model,@RequestParam Long id) {
        model.addAttribute("role", roleService.selectById(id));
        return "admin/role/roleEdit";
    }
    @RequestMapping(value = "/edit", method = {RequestMethod.POST })
    @ResponseBody
    public Object edit(Role role) {
        roleService.updateById(role);
        return renderSuccess("更新成功");
    }
    @RequestMapping(value = "/deleteRole", method = {RequestMethod.POST })
    @ResponseBody
    public Object delete(String ids) {

        roleService.deleteBatchByIds(ids);
        return renderSuccess("删除成功！");
    }
}
